import { useEffect} from "react";
import { Route, Routes ,useNavigate} from "react-router-dom";
import Home from "../home";
import Login from "../login";
import { useSelector , useDispatch} from 'react-redux';
import { loginChangeState } from "../store/user";

const Router = () => {
    const nav = useNavigate();
    let data = localStorage.getItem("logined");
    data = JSON.parse(data);
    const isLogin = useSelector(state => state.user);
    const dispatch = useDispatch();
    useEffect(() => {
       if (data  !== null) {
            dispatch(loginChangeState(),{
                ...isLogin,
                isLogin : true,
            })
            localStorage.removeItem("logined");
        } 
        
        console.log("data:",data)
    },[])
    useEffect(() => {
        if (data === null?true:!data.isLogin) {
            // console.log('执行了');
            console.dir(nav)
            nav('/login',{replace:true});
        }
        else{
            nav('/',{replace:true});
            localStorage.setItem("logined",JSON.stringify(data));
        }
    },[isLogin.isLogin])
    
    
    
    
    return (
        <Routes>
            <Route path="/login" element={<Login />} />
            <Route path="*" element={<Home />} />
        </Routes>
    );
};

export default Router;
